package com.controller;

import com.alibaba.fastjson.JSON;
import com.pojo.StorageRecord;
import com.pojo.Supplier;
import com.service.RecordService;
import com.service.SupplierService;
import com.util.Page;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/storageRecord")
public class RecordController {
    private static Logger logger=Logger.getLogger(RecordController.class);
    @Resource
    private RecordService recordService;

    @Resource
    private SupplierService supplierService;

    @RequestMapping("/list")
    public String getRecordList(Model model, String queryGoodsName,
                                Integer querySupplierId,Integer queryPayStatus,@RequestParam(defaultValue = "1") int pageIndex){
        List<StorageRecord> storageRecordList=null;
        Page<StorageRecord> storageRecordPage = new Page<StorageRecord>();

        try {
            //设置总数据量
            storageRecordPage.setTotalPage(recordService.count(queryGoodsName, querySupplierId,queryPayStatus));
            //设置总页数
            storageRecordPage.setTotalPageSize();
            //当前页
            storageRecordPage.setPageIndex(pageIndex);
            //获取当前页数据
            storageRecordPage.setList(recordService.select(queryGoodsName,querySupplierId,queryPayStatus,storageRecordPage.getPageIndex(), storageRecordPage.getPageSize()));

            storageRecordList=recordService.select(queryGoodsName,querySupplierId,queryPayStatus,storageRecordPage.getPageIndex(),storageRecordPage.getPageSize());
            model.addAttribute("storageRecordList",storageRecordList);

            List<Supplier> supplierList=null;
            supplierList=supplierService.supplierList();

            model.addAttribute("supplierList",supplierList);

            model.addAttribute("queryGoodsName", queryGoodsName);
            model.addAttribute("querySupplierId", querySupplierId);
            model.addAttribute("queryPayStatus",queryPayStatus);

            model.addAttribute("storageRecordList", storageRecordPage.getList());
            model.addAttribute("totalPageCount", storageRecordPage.getTotalPageSize());
            model.addAttribute("totalCount", storageRecordPage.getTotalPage());
            model.addAttribute("currentPageNo", storageRecordPage.getPageIndex());

        }catch (Exception e){
            e.printStackTrace();
            return "redirect:/storageRecord/toSysError";
        }
        return "/storageRecord/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "storageRecord/add";
    }

    @RequestMapping("/add")
    public String doAdd(StorageRecord storageRecord){
        if(recordService.add(storageRecord)){
            return "redirect:/storageRecord/list";
        }
        return "/storageRecord/add";
    }

    @ResponseBody
    @RequestMapping("/simpleList")
    public Object simpleList(){
        List<Supplier> supplierList=supplierService.supplierList();
        if(supplierList==null){
            logger.debug("未获取供货商信息!");
        }
        return JSON.toJSONString(supplierList);
    }

    @RequestMapping("/view/{id}")
    public String view(HttpServletRequest request,@PathVariable String id){
        request.setAttribute("storageRecord",recordService.getStorageRecordById(id));
        return "/storageRecord/view";
    }


    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }
}
